package com.terry.proxy.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.terry.proxy.application.cache.DBServerCacheImpl;
import com.terry.proxy.application.dao.ProxyClientDao;
import com.terry.proxy.application.dto.ProxyClientInsertDTO;
import com.terry.proxy.application.entity.ProxyClient;
import com.terry.proxy.application.entity.ProxyClientTunnel;
import com.terry.proxy.application.service.ProxyClientTunnelService;
import com.terry.proxy.application.service.impl.ProxyClientServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 客户端(ProxyClient)表控制层
 *
 * @author terry
 * @since 2022-12-14 12:16:18
 */
@RestController
@RequestMapping("/proxyClient")
public class ProxyClientController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ProxyClientServiceImpl proxyClientService;
    @Resource
    private ProxyClientTunnelService proxyClientTunnelService;
    @Resource
    private ProxyClientDao proxyClientDao;
    @Resource
    private DBServerCacheImpl dbServerCache;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param proxyClient 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectPage(Page<ProxyClient> page, ProxyClient proxyClient) {
        return success(this.proxyClientService.page(page, new QueryWrapper<>(proxyClient)));
    }

    /**
     * 分页查询所有数据
     *
     * @param proxyClient 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public R selectAll(ProxyClient proxyClient) {
        return success(this.proxyClientService.list(new QueryWrapper<>(proxyClient)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.proxyClientService.getById(id));
    }

    /**
     * 修改数据
     *
     * @param proxyClient 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody ProxyClient proxyClient) {
        dbServerCache.updateClient(proxyClient);
        return success(this.proxyClientService.updateById(proxyClient));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Integer> idList) {
        dbServerCache.removeClient(idList.get(0));
        return success(this.proxyClientService.removeByIds(idList));
    }

    /**
     * 新增客户端、隧道
     *
     * @param proxyClientInsertDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insertUnion")
    public R insertUnion(@RequestBody ProxyClientInsertDTO proxyClientInsertDTO) {
        if (proxyClientService.insertUnion(proxyClientInsertDTO)) {
            return success("插入成功！");
        }
        return failed("插入失败！");
    }

    /**
     * 删除客户端、隧道
     *
     */
    @DeleteMapping("/deleteUnion/{id}")
    public R deleteUnion(@PathVariable Integer id) {
        this.proxyClientService.removeById(id);
        LambdaQueryWrapper<ProxyClientTunnel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProxyClientTunnel::getClientId, id);
        this.proxyClientTunnelService.remove(wrapper);
        dbServerCache.removeClient(id);
        return success("删除成功！");
    }
}

